package com.taxshare.response.beans;

import cn.hutool.core.util.StrUtil;
import com.taxshare.response.utils.PropertiesUtil;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

/**
 * 返回对象
 *
 */
@Data
@ApiModel(value = "Api返回对象", description = "Api返回对象")
@SuppressWarnings("all")
public class Response<T> {

	private static final long serialVersionUID = 1854514035256811234L;

	public static final String SUCCESS = "T0000";

	public static final String SUCCESS_MSG = "操作执行成功";

	public static final String ERROR = "S9999";

	public static final String ERROR_MSG = "系统服务异常";

	public static final String UNKNOW_MSG = "无法获取该异常编码消息";

	@ApiModelProperty(value = "返回代码")
	private String code;

	@ApiModelProperty(value = "返回信息")
	private String msg;

	@ApiModelProperty(value = "数据视图")
	private T data = null;

	@ApiModelProperty(value = "异常信息")
	private String stackTrace;

	@ApiModelProperty(value = "是否异常")
	private boolean fail = false;

	public Response() {
	}

	private Response(String code, String msg, T data) {
		this.code = code;
		this.msg = msg;
		this.data = data;
	}

	public Response(String code, String msg, String stackTrace, boolean fail) {
		this.code = code;
		this.msg = msg;
		this.stackTrace = stackTrace;
		this.fail = fail;
	}

	public Response(String code, String msg) {
		this(code, msg, null);
	}

	public static <T> Response<T> success(T data) {
		return new Response(SUCCESS, SUCCESS_MSG, data);
	}

	public static <T> Response<T> success() {
		return new Response(SUCCESS, SUCCESS_MSG);
	}

	public static <T> Response<T> error() {
		return new Response(ERROR, ERROR_MSG);
	}

	public static <T> Response<T> error(String code) {
		return new Response(code, info(code));
	}

	public static <T> Response<T> errorf(String code, String... msgs) {
		return new Response(code, info(code, msgs));
	}

	public static <T> Response<T> error(String code, String msg) {
		return new Response(code, msg, null);
	}

	public static <T> Response<T> error(String code, String msg, T data) {
		return new Response(code, msg, data);
	}

	public static <T> Response<T> error(String code, String msg, String stackTrace) {
		return new Response<>(code, msg, stackTrace, true);
	}

	public static <T> Response<T> warn(String code) {
		return new Response(code, info(code));
	}

	public static <T> Response<T> warn(String code, String msg) {
		return new Response(code, msg);
	}

	public static <T> Response<T> warn(String code, String msg, T data) {
		return new Response(code, msg, data);
	}

	public static <T> Response<T> warnf(String code, String... msgs) {
		return new Response(code, info(code, msgs));
	}

	public static String info(String code, String... msgs) {
		return String.format(info(code), msgs);
	}

	public static String info(String code) {
		return StrUtil.blankToDefault(PropertiesUtil.getItem(code), UNKNOW_MSG);
	}
}
